package com.quanshu.change.person.view;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.quanshu.change.person.adapter.SeckillGoodsBuyAdapter;
import com.quanshu.change.person.entity.SeckillGoodsBuyEntity;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.api.ShopInterface;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.support.utils.LogUtil;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.exchange.ui.base.MyApplication;
import com.quanshu.shop.base.tool.BaseTool;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link SeckillGoodsGet2Fragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class SeckillGoodsGet2Fragment extends Fragment {


    @BindView(R.id.rl)
    RecyclerView rl;
    Unbinder unbinder;
    private SeckillGoodsBuyAdapter seckillGoodsBuyAdapter;
    private List<SeckillGoodsBuyEntity> seckillGoodsBuyEntityList;

    public SeckillGoodsGet2Fragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment SeckillGoodsGet2Fragment.
     */
    // TODO: Rename and change types and number of parameters
    public static SeckillGoodsGet2Fragment newInstance() {
        SeckillGoodsGet2Fragment fragment = new SeckillGoodsGet2Fragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_seckill_goods_get2, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private void initView() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rl.setLayoutManager(linearLayoutManager);
        getData();
    }

    //获取秒杀商品寄售失败明细
    private void getData() {
        if (MyApplication.getInstance().getCurrentUser() != null) {
            NetClient.getInstance().getShopService(ShopInterface.class)
                    .getHotGoods(HotGoodsActivity.HOT_SECKILL_ISINN, MyApplication.getInstance().getCurrentUser().getMemberid() + "", HotGoodsActivity.HOT_SECKILL_SELL_TYPE, HotGoodsActivity.HOT_SECKILL_SELL3_ISTYPE)
                    .compose(new BaseTransformer<>((BaseActivity) getActivity(), true, false))
                    .subscribe(new BaseObserver<BaseResponse<List<SeckillGoodsBuyEntity>>>() {
                        @Override
                        public void onSuccess(BaseResponse<List<SeckillGoodsBuyEntity>> response) {
                            if (response.getCode() == 1) {
                                seckillGoodsBuyEntityList = response.getData();
                                seckillGoodsBuyAdapter = new SeckillGoodsBuyAdapter(seckillGoodsBuyEntityList);
                                seckillGoodsBuyAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                                    @Override
                                    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                                        switch (view.getId()) {
                                            case R.id.root_cl:
                                                Bundle bundle = new Bundle();
                                                bundle.putString(HotGoodsActivity.GOODS_ID, seckillGoodsBuyEntityList.get(position).getOid());
                                                bundle.putString(HotGoodsActivity.GOODS_TYPE, "3");
                                                bundle.putInt(SeckillGoodsSellDetialActivity.SECKILL_TYPE,2);
                                                BaseTool.goActivity(getActivity(), SeckillGoodsSellDetialActivity.class, bundle);
                                                break;
                                        }
                                    }
                                });
                                rl.setAdapter(seckillGoodsBuyAdapter);
                            }
                        }

                        @Override
                        public void onFailure(Throwable e) {
                            LogUtil.printException(e);
                        }
                    });
        }
    }
}
